﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellPrimitiveSerializer_h
#define CellPrimitiveSerializer_h

#include "CellPrerequisites.h"
#include "CellSerializer.h"
#include "CellStuff.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class PrimitiveSerializerSubstance;
class PrimitiveSerializerSubstanceFactory;

/** 原语序列化。
@author
	Jiangwei Xu
*/
class _CellExport PrimitiveSerializer : public Serializer, public GeneralAlloc
{
public:
	/** 序列化器版本描述。
	*/
	class _CellExport Version : public GeneralAlloc
	{
	public:
		/**
		*/
		Version(void);

		/**
		*/
		Version(const Version& other);

		/**
		*/
		Version(int major, int minor);

		/**
		*/
		virtual ~Version(void);

	public:
		/**
		*/
		inline int getMajor(void) const { return mMajor; }

		/**
		*/
		inline int getMinor(void) const { return mMinor; }

	private:
		int mMajor;
		int mMinor;

		friend class PrimitiveSerializer;
	};

	/** 构造函数。
	*/
	PrimitiveSerializer(const Version& version);

	/** 析构函数。
	*/
	virtual ~PrimitiveSerializer(void);

public:
	/***/
	static const Version VERSION;

	/**
	*/
	bool write(SerializerStream& out, Serializable* in);

	/**
	*/
	bool read(Serializable* out, SerializerStream& in);

protected:
	/**
	*/
	void serializeVersion(SerializerStream& strm, const Version& version) const;

	/**
	*/
	void deserializeVersion(Version& version, SerializerStream& strm);

	/**
	*/
	bool checkCompatibility(const Version& version);

private:
	Version mVersion;
	PrimitiveSerializerSubstanceFactory* mFactory;
};


/**
*/
class _CellPrivate PrimitiveSerializerSubstance : public GeneralAlloc
{
public:
	/**
	*/
	PrimitiveSerializerSubstance(const PrimitiveSerializer::Version& version);

	/**
	*/
	virtual ~PrimitiveSerializerSubstance(void);

public:
	/**
	*/
	virtual void serialize(SerializerStream& strm, Primitive* primitive) = 0;

	/**
	*/
	virtual void deserialize(Primitive* primitive, SerializerStream& strm) = 0;

	/**
	*/
	const String& parseLiteralBase(LiteralBase literal);
	/**
	*/
	LiteralBase parseLiteralBase(const String& literal);

	/**
	*/
	const String& parseStuffType(Stuff::StuffType type);
	/**
	*/
	Stuff::StuffType parseStuffType(const String& type);

protected:
	PrimitiveSerializer::Version mVersion;

private:
	String mStuffTypeNameBuf;
	String mLiteralBaseNameBuf;

	friend class PrimitiveSerializerSubstanceFactory;
};


/**
*/
class _CellPrivate PrimitiveSerializer_1_0 : public PrimitiveSerializerSubstance
{
public:
	/**
	*/
	PrimitiveSerializer_1_0(void);

	/**
	*/
	virtual ~PrimitiveSerializer_1_0(void);

public:
	/**
	*/
	virtual void serialize(SerializerStream& strm, Primitive* primitive);

	/**
	*/
	virtual void deserialize(Primitive* primitive, SerializerStream& strm);

protected:
	/**
	*/
	void serialize(SerializerStream& strm, const Stuff::StuffType type,
		const String& value, const LiteralBase literal);

	/**
	*/
	void deserialize(Primitive* primitive, Stuff::StuffType type, const String& value, LiteralBase literal);

	/**
	*/
	void deserializeDialect(Primitive* primitive, const String& data);

	/**
	*/
	void revise(char* out, const char* in);

	friend class PrimitiveSerializerSubstanceFactory;
};


/**
*/
class _CellPrivate PrimitiveSerializerSubstanceFactory : public GeneralAlloc
{
public:
	/**
	*/
	PrimitiveSerializerSubstanceFactory(void);

	/**
	*/
	virtual ~PrimitiveSerializerSubstanceFactory(void);

public:
	/**
	*/
	PrimitiveSerializerSubstance* create(const PrimitiveSerializer::Version& version);

	/**
	*/
	void destroy(PrimitiveSerializerSubstance* substance);

protected:
	PrimitiveSerializer_1_0* mV_1_0;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellPrimitiveSerializer_h
